package com.trade.action;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import com.trade.exception.BusinessException;
import com.trade.form.SupplierForm;
import com.trade.model.Supplier;
import com.trade.service.ISupplierSevice;

/**
 * @author Lin Yang
 * 
 */
public class SupplierAction extends DispatchAction {
    private ISupplierSevice supplierSevice;

    public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SupplierForm supplierForm = (SupplierForm) form;
        String name = supplierForm.getName();
        List<Supplier> suppliers = Collections.emptyList();
        try {
            suppliers = supplierSevice.findAllLike(name);
        } catch (BusinessException e) {
            e.printStackTrace();
            ActionErrors errors = new ActionErrors();
            errors.add("fail", new ActionMessage("查询出错，请联系管理员！", false));
            this.addErrors(request, errors);
            return mapping.findForward("fail");
        }

        if (!suppliers.isEmpty()) {
            request.setAttribute("suppliers", suppliers);
            return mapping.findForward("supplierList");
        } else {
            ActionErrors errors = new ActionErrors();
            errors.add("fail", new ActionMessage("没有查询结果！", false));
            this.addErrors(request, errors);
            return mapping.findForward("fail");
        }
    }

    public void setSupplierSevice(ISupplierSevice supplierSevice) {
        this.supplierSevice = supplierSevice;
    }

}
